//
//  ZNDatePicerView.m
//  GuoBinTeam
//
//  Created by lizhenning on 15/1/8.
//  Copyright (c) 2015年 lizhenning. All rights reserved.
//

#import "ZNDatePickerView.h"

#define ColorRGBA(_red,_green,_blue,_alpha) \
[UIColor colorWithRed:(_red/255.0) green:(_green/255.0) blue:(_blue/255.0) alpha:_alpha]

@interface ZNDatePickerView()
{
    UIView *viewPickerBg;
    UIDatePicker *picker;
}

@end

@implementation ZNDatePickerView

@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame andTimeLine:(NSDate *)date Model:(NSInteger)model Max:(NSDate *)maxDate Min:(NSDate *)minDate
{
    self = [self initWithFrame:frame];
    
    if (self)
    {
        
        self.backgroundColor = ColorRGBA(22, 22, 22, 0.8);
        
        viewPickerBg = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 256)];
        [viewPickerBg setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:viewPickerBg];
        
        picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 216)];
        
        [picker setTimeZone:[NSTimeZone defaultTimeZone]];
        
        
        if (date == nil)
        {
            [picker setDate:[NSDate date]];
        }else
        {
            [picker setDate:date animated:YES];
        }
        
        if (maxDate)
        {
            [picker setMaximumDate:maxDate];
            
        }
        
        if (minDate)
        {
            [picker setMinimumDate:minDate];
        }
        
        [picker setDatePickerMode:model];
        
        [viewPickerBg addSubview:picker];
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
        [toolBar setUserInteractionEnabled:YES];
        toolBar.barStyle = UIBarStyleDefault;
        
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"  取消" style:UIBarButtonItemStylePlain target:self action:@selector(btnCancelPressed)];
        [left setTintColor:[UIColor blackColor]];
        
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定  " style:UIBarButtonItemStylePlain target:self action:@selector(btnDonePressed)];
        [right setTintColor:[UIColor blackColor]];
        
        [toolBar setItems:@[left,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil],right]];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 1)];
        [viewLine setBackgroundColor:ColorRGBA(227, 227, 227, 1)];
        
        
        [viewPickerBg addSubview:toolBar];
        [viewPickerBg addSubview:viewLine];
       
        
        
        
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame andTimeLine:(NSDate *)date
{
    self = [self initWithFrame:frame];
    
    if (self)
    {
        
        self.backgroundColor = ColorRGBA(22, 22, 22, 0.8);
        
        viewPickerBg = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 256)];
        [viewPickerBg setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:viewPickerBg];
        
        picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 216)];
        
        [picker setTimeZone:[NSTimeZone defaultTimeZone]];
        
        if (date == nil)
        {
            [picker setDate:[NSDate date]];
        }else
        {
            [picker setDate:date animated:YES];
        }
        
        
        [picker setMaximumDate:[NSDate date]];
        [picker setDatePickerMode:UIDatePickerModeDate];
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 40)];
        [toolBar setUserInteractionEnabled:YES];
        toolBar.barStyle = UIBarStyleDefault;
        
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"  取消" style:UIBarButtonItemStylePlain target:self action:@selector(btnCancelPressed)];
        [left setTintColor:[UIColor blackColor]];
        
        UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定  " style:UIBarButtonItemStylePlain target:self action:@selector(btnDonePressed)];
        [right setTintColor:[UIColor blackColor]];
        
        [toolBar setItems:@[left,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil],right]];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 40, frame.size.width, 1)];
        [viewLine setBackgroundColor:ColorRGBA(227, 227, 227, 1)];
        
        
        [viewPickerBg addSubview:toolBar];
        [viewPickerBg addSubview:viewLine];
        [viewPickerBg addSubview:picker];
        
        
        
    }
    
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if (newSuperview == nil)
    {
        return;
    }
    
    CGRect frameBg = viewPickerBg.frame;
    
    frameBg.origin.y = self.frame.size.height - 256;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        viewPickerBg.frame = frameBg;
        
    }];
    
}

- (void)btnCancelPressed
{
    
    CGRect frameBg = viewPickerBg.frame;
    frameBg.origin.y = self.frame.size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        viewPickerBg.frame = frameBg;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}

- (void)btnDonePressed
{
    if ([_delegate respondsToSelector:@selector(datePickerResult:)])
    {
        [_delegate datePickerResult:picker.date];
    }
    
    [self btnCancelPressed];
    
}


@end
